package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.OtherPurchaseRecordStatus;
import com.jiantong.work.impl.dao.OtherPurchaseRecordDao;
import com.jiantong.work.model.OtherPurchaseRecord;
import com.jiantong.work.service.OtherPurchaseRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by niufei on 2017/4/8.
 *
 */
@Slf4j
@Service
@RpcProvider
public class OtherPurchaseRecordReadServiceImpl implements OtherPurchaseRecordReadService{

    private final OtherPurchaseRecordDao otherPurchaseRecordDao;
    private final BreedFactoryDao breedFactoryDao;
    @Autowired
    public OtherPurchaseRecordReadServiceImpl(OtherPurchaseRecordDao otherPurchaseRecordDao, BreedFactoryDao breedFactoryDao) {
        this.otherPurchaseRecordDao = otherPurchaseRecordDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    @Override
    public Response<Paging<OtherPurchaseRecord>> pagingOtherPurchaseRecordsFarm(Integer pageNo, Integer pageSize,Long breedFactoryId,  Integer status) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            criteria.put("breedFactoryId",breedFactoryId);
            if(status!=null) {
                criteria.put("status",status);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(otherPurchaseRecordDao.pagingOtherPurchaseRecordsFarm(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging OtherPurchaseRecord, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("OtherPurchaseRecord.paging.fail");
        }
    }

    @Override
    public Response<Paging<OtherPurchaseRecord>> pagingOtherPurchaseRecordsGov(Integer pageNo, Integer pageSize, String breedFactoryName, Date fromDate, Date toDate) {
        return null;
    }

    @Override
    public Response<Paging<OtherPurchaseRecord>> pagingOtherPurchaseRecordsGov(Integer pageNo , Integer pageSize ,  Long breedFactoryId ,Integer breedType, Date fromDate , Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            List <Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            //在这里根据breedType赛选一下养殖场,从breedFactoryIds中去掉不是这个养殖类型的养殖场ID
            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if(!breedFactoryIds.isEmpty()) {
                    map.put("breedFactoryIds", breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }

            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate!=null && toDate!=null){
                criteria.put("fromDate",fromDate);
                criteria.put("toDate",toDate);
            }
            criteria.put("status", OtherPurchaseRecordStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(otherPurchaseRecordDao.pagingOtherPurchaseRecordsGov(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging OtherPurchaseRecord, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("OtherPurchaseRecord.paging.fail");
        }
    }

    @Override
    public Response<Paging<OtherPurchaseRecord>> pagingOtherPurchaseRecordsGov(Integer pageNo, Integer pageSize, Map map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(otherPurchaseRecordDao.pagingOtherPurchaseRecordsGov(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging OtherPurchaseRecord, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("OtherPurchaseRecord.paging.fail");
        }

}

    @Override
    public Response<OtherPurchaseRecord> findOtherPurchaseRecordById(Long id) {
        try{
            OtherPurchaseRecord otherPurchaseRecord = otherPurchaseRecordDao.findById(id);
            if(otherPurchaseRecord == null){
                log.error("otherPurchaseRecord(id={}) is not found", id);
                return Response.fail("otherPurchaseRecord.not.found");
            }
            return Response.ok(otherPurchaseRecord);
        }catch (Exception e){
            log.error("failed to find otherPurchaseRecord(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("otherPurchaseRecord.find.fail");
        }
    }

    @Override
    public String findCurrentMaxNumber(String number) {
        return otherPurchaseRecordDao.findCurrentMaxNumber(number);
    }

    @Override
    public Response<Double> sumTotalPriceForWeb(Map<String, Object> map) {
        try{
            return Response.ok(otherPurchaseRecordDao.sumTotalPriceForWeb(map));
        }catch (Exception e){
            log.error("failed to sum total price ,case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to sum total price");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findOtherPurchaseInfoWeb(Map<String, Object> map) {
        try{
            return Response.ok(otherPurchaseRecordDao.findOtherPurchaseInfoWeb(map));
        }catch (Exception e){
            log.error("failed to find other purchase info web , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find other purchase info web");
        }
    }

    @Override
    public Response<Map<String ,Integer>> findStatusById(Long id) {
        try{
            Integer status = otherPurchaseRecordDao.findStatusById(id);
            if(status == null){
                log.error("otherPurchaseRecord status (id={}) is not found", id);
                return Response.fail("otherPurchaseRecord status.not.found");
            }
            Map<String ,Integer> map = Maps.newHashMap();
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find otherPurchaseRecord status(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("otherPurchaseRecord status.find.fail");
        }
    }
}
